import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../common.dart';

class Announcement extends StatefulWidget {
  @override
  State<Announcement> createState() => _SettingPageState();
}

class _SettingPageState extends State<Announcement> {
  RxList announcementData = [].obs;
  RxList categoryData = [].obs;
  RxInt index = 0.obs;

  getAnnouncement(id) {
    getData(url: '/announcement', data: {'category_id': '$id'}).then((res) {
      announcementData.value = res;
    });
  }

  getCategory() {
    getData(url: '/announcement/category', load: false).then((res) {
      categoryData.value = res;
      getAnnouncement(0);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getCategory();
  }

  @override
  Widget build(BuildContext context) {
    con(context);

    return Scaffold(
      appBar: AppBar(
        leading: buildBackButton(context),
        backgroundColor: Color(0xffffffff),
        title: wText(text: '平台公告', size: 18),
        centerTitle: true,
      ),
      body: ListView(
        children: [
          SizedBox(
            height: 30,
            child: Obx(
              () => ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  for (var i = 0; i < categoryData.length; i++)
                    GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        index.value = i;
                        getAnnouncement(categoryData[i]['id']);
                      },
                      child: Container(
                        margin: EdgeInsets.only(left: 10),
                        alignment: Alignment.center,
                        padding: EdgeInsets.symmetric(horizontal: 10),
                        decoration: BoxDecoration(
                          color:
                              index.value == i
                                  ? Color(0xff0F172B)
                                  : Color(0XFFF8F9FD),
                          borderRadius: BorderRadius.circular(30),
                        ),
                        child: Text(
                          '${categoryData[i]['title']}',
                          style: TextStyle(
                            fontSize: 14,
                            color:
                                index.value == i
                                    ? Color(0xffffffff)
                                    : Color(0XFF0F172B),
                          ),
                        ),
                      ),
                    ),
                ],
              ),
            ),
          ),
          Divider(color: Color(0XFFDDDDDD), height: 30),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 15),
            child: Obx(
              () => Column(
                children: [
                  if (announcementData.isEmpty) isdata(),

                  for (var item in announcementData)
                    Column(
                      children: [
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap:
                              () => {
                                toNamed(
                                  '/web_tabs/announcement/detail?uuid=${item['uuid']}',
                                ),
                              },
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Container(
                                    width: 200,
                                    height: 50,
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              padding: EdgeInsets.symmetric(
                                                horizontal: 5,
                                                vertical: 2,
                                              ),
                                              decoration: BoxDecoration(
                                                color: Color(0XFF0F172B),
                                                borderRadius:
                                                    BorderRadius.circular(3),
                                              ),
                                              child: Text(
                                                '${item['category']['title']}',
                                                style: TextStyle(
                                                  fontSize: 11,
                                                  color: Color(0XFFFFFFFF),
                                                ),
                                              ),
                                            ),
                                            SizedBox(width: 5),
                                            item['title'].length > 9
                                                ? Text(
                                                  '${item['title'].substring(0, 9)}',
                                                )
                                                : Text('${item['title']}'),
                                          ],
                                        ),
                                        SizedBox(height: 2),
                                        item['title'].length > 9
                                            ? Text(
                                              "${item['title'].substring(9, item['title'].length)}",
                                              overflow: TextOverflow.ellipsis,
                                              maxLines: 1,
                                            )
                                            : Text(''),
                                      ],
                                    ),
                                  ),
                                  Text(
                                    '${item['created_at']}',
                                    style: TextStyle(
                                      fontSize: 11,
                                      color: Color(0XFF999999),
                                    ),
                                  ),
                                ],
                              ),
                              ClipRRect(
                                borderRadius: BorderRadius.circular(
                                  7.0,
                                ), // 设置圆角大小
                                child: Image.network(
                                  '${item['thumb']}',
                                  width: 105,
                                  height: 70,
                                  fit: BoxFit.fill,
                                  errorBuilder: (context, error, stackTrace) {
                                    return SizedBox(width: 105);
                                  },
                                ),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(height: 15),
                      ],
                    ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
